Loader.loadLib("ogreLib");

Holder.getObject("ogre", "Scene");
Holder.getObject("head", "Entity");
Holder.getObject("ninz", "Entity");
Holder.getObject("node", "Node");
Holder.getObject("niNode", "Node");
Holder.getObject("groundNode", "Node");
Holder.getObject("ground", "Plane");
Holder.getObject("longWall", "Plane");
Holder.getObject("shortWall", "Plane");
Holder.getObject("light", "SpotLight");
Holder.getObject("camera", "Camera");
Holder.getObject("camera2", "Camera");

//ogre.run()

camera.xPos = 1000;
camera.yPos = 100;
camera.zPos = 500;
camera.xLookAt = 0;
camera.yLookAt = 0;
camera.zLookAt = 0;


camera2.xPos = 100;
camera2.yPos = 222;
camera2.zPos = 300;
camera2.xLookAt = 0;
camera2.yLookAt = 0;
camera2.zLookAt = -300;

ogre.mainCamId = "camera";
ogre.addObject(camera);
ogre.addObject(camera2);

ogre.aGreen = 0;
ogre.aBlue = 0;
ogre.aRed = 0;

light.lightType = 2;
light.difRed = 0.7;
light.difGreen = 1;
light.specBlue = 0;
light.specRed = 1;
light.difBlue = 0.2;
light.yPos = 100;
light.xPos = 100;

light.dirX = -10;
light.dirY = -10;
light.dirz = 0;

light.innerAng = 60;
light.outerAng = 85;
light.fallOff = 4;
ogre.addObject(light)

//node.id = "node";
node.parentNodeId = "root";
groundNode.parentNodeId = "root";
//node.xPos = -75;
niNode.parentNodeId = "root";
ogre.addObject(node);
ogre.addObject(groundNode);
niNode.yAng = -90;
ogre.addObject(niNode);

niNode.xPos = - 100;
niNode.yPos = - 40;

//head.id = "Head";
head.model = "ogrehead.mesh";
head.nodeId = "node"
head.castShadows = true;
ogre.addObject(head);

ninz.model = "ninja.mesh";
ninz.nodeId = "niNode"
ninz.castShadows = true;
ogre.addObject(ninz);


// -- ground
ground.xNormalPos = 0;
ground.yNormalPos = 1;
ground.zNormalPos = 0;

ground.xUpPos = 1;
ground.yUpPos = 0;
ground.zUpPos = 0;

ground.width = 1000;
ground.height = 2000;
ground.dist = 0;
ground.castShadows = false;
ground.nodeId = "groundNode";
ground.materialName = "Examples/Rockwall";
ogre.addObject(ground);

// -- ground
longWall.xNormalPos = 0;
longWall.yNormalPos = 0;
longWall.zNormalPos = 1;

longWall.xUpPos = 1;
longWall.yUpPos = 0;
longWall.zUpPos = 0;

longWall.width = 400;
longWall.height = 2000;
longWall.dist = 500;
longWall.castShadows = false;
longWall.nodeId = "groundNode";
longWall.materialName = "Examples/Rockwall";
ogre.addObject(longWall);

// -- ground
shortWall.xNormalPos = 1;
shortWall.yNormalPos = 0;
shortWall.zNormalPos = 0;

shortWall.xUpPos = 0;
shortWall.yUpPos = 0;
shortWall.zUpPos = 1;

shortWall.width = 400;
shortWall.height = 1000;
shortWall.dist = 1000;
shortWall.castShadows = false;
shortWall.nodeId = "groundNode";
shortWall.materialName = "Examples/Rockwall";
ogre.addObject(shortWall);

groundNode.yPos = -40;

var rCnt = 0;
var dir = 0;
var adir = 0;

function move(elTime) {
    //println("time = " + elTime);
    rCnt = rCnt + elTime;
   // if (rCnt >= 400) {
     //   rCnt = 0;
        //node.xPos = node.xPos + 1.2;
//        node.yPos = node.yPos + 0.001 * elTime;
        node.xPos = node.xPos + 0.001 * elTime;
//    //}
    if (dir == 0) {
        node.xScale = node.xScale - 0.001 * elTime;
        if (node.xScale <= 0.5) dir = 1;
    } else {
        node.xScale = node.xScale + 0.0001 * elTime;
        if (node.xScale >= 1.5) dir = 0;
        //ogre.stop();
    }

   // node.xAng = node.xAng + 0.01 * elTime;
    //node.yAng = node.yAng + 0.01 * elTime;
    node.zAng = node.zAng + 0.01 * elTime;

    if (adir == 0) {
        ogre.aRed = ogre.aRed + 0.001 * elTime;
        if (ogre.aRed >= 1) adir = 1;
    } else {
        ogre.aRed = ogre.aRed - 0.001 * elTime;
        if (ogre.aRed <= 0.1) adir = 0;
    }

    //camera.yPos = camera.yPos - elTime;
    //camera.applyPos();

//    ogre.aGreen = 0;
//    ogre.aBlue = 0;
//    ogre.aRed = 1;
//   light.yPos = light.yPos + 0.1 * elTime;
//    light.dirX = 10;
}

ogre.onFrameRendered.connect(move);
ogre.onKeyPress.connect(keyp)

var paused = false;
function keyp(key) {
    if (key == 2) {
        ogre.mainCamId = "camera";
    }
    if (key == 3) {
        ogre.mainCamId = "camera2";
    }
    if (key == 4) {
        if (paused) {
            ogre.resume();
        } else {
            ogre.pause();
        }
        paused = !paused;
    }
}
